package generationgraphe;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TestObserverObservable implements ActionListener{
	private JButton bouton1, bouton2, bouton3;
	private JPanel center, panelBouton, graphique;
	private JFrame frame;
	private PlageCellule modele;
        private Table table;
        private JTabbedPane tab;
        private Object[] cellules;
	
	public TestObserverObservable(){
		
		frame = new JFrame("Test des Obse/Obervable");
		frame.setSize(950,600);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		center = new JPanel();
                graphique = new JPanel();
                tab = new JTabbedPane();
		panelBouton = new JPanel(new FlowLayout());
		bouton1 = new JButton("Melanger");
		panelBouton.add(bouton1);
		bouton1.setActionCommand("melange");
		bouton1.addActionListener(this);
		bouton2 = new JButton("Nouvelle Vue");
		panelBouton.add(bouton2);
		bouton2.setActionCommand("nouvelle");
		bouton2.addActionListener(this);
		bouton3 = new JButton("Vue Externe");
		panelBouton.add(bouton3);
		bouton3.setActionCommand("externe");
		bouton3.addActionListener(this);
		frame.getContentPane().add(center,"West");
		frame.getContentPane().add(panelBouton, "North");
                frame.getContentPane().add(graphique,"East");
		creerVueExterne();
		frame.setVisible(true);
                graphique.add(tab, "South");
                tab.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 
	}

	private void creerVue() {
            JComponent centre = new JPanel();
            ImageIcon icon = new ImageIcon("");
            modele = new PlageCellule();
            IVueTableau vue = new VueBar();
            GrapheurTabEntiers graph = new GrapheurTabEntiers(vue, modele);
            //modele.addObserver(graph);
            Table.getInstance().addObserver(graph);
            centre.add(graph);
            centre.validate();
            tab.addTab("tab0",icon, centre,"Does nothing");
	}
        
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("melange")){
			//modele.melange();
//                    PlageCellule pc= new PlageCellule();
//                    pc.affiche();
//                    List<Object> ob = Table.getInstance().getSeleted();
//                    for(Object o:ob){
//                        System.out.println("Les valeurs "+o);
//                    }
         
                }
                    
		else if(e.getActionCommand().equals("nouvelle")){
			creerVue();
		}
		else if(e.getActionCommand().equals("externe")){
			creerVueExterne();
		}
	}
// pour creer une vue externe de la frame principal
	private void creerVueExterne() {
		// TODO Auto-generated method stub
            
            frame.add(Table.getInstance().getTable());
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TestObserverObservable();
	}

	
}
